/**
 * 显示错误信息
 * @param mesg
 */
function showErrorInfo(mesg) {
    $('.tips span').text(mesg);
    $('.tips').slideDown();
    $(this).attr('disabled', 'disabled');
    setTimeout(() => {
        $('.tips').slideUp();
        $(this).removeAttr('disabled')
    }, 3000);
}

function showAdminErrorInfo(mesg) {
    $("#errorSpan").html(mesg);
    $("#errorInfo").show();
    $("#adminAcLoginId").attr('disabled',true);
    $("#adminAcLoginId").attr('disabled', 'disabled');
    setTimeout(() => {
        $("#errorInfo").hide();
        $("#adminAcLoginId").attr('disabled',false);
        $("#adminAcLoginId").removeAttr('disabled')
    }, 3500);
}

/**
 * 管理端回车键登录
 */
function adminKeyLogin() {
    //判断是否为回车键按下
    if(event.keyCode == 13){
        adminAcLogin();
    }
}

/**
 * 用户端回车键登录
 * @param type 登录类型（00账号登录，01税号登录）
 */
function userKeyLogin(type) {

    //判断是否为回车键按下
    if(event.keyCode == 13){
        if (type == '00'){
            acLogin();
        }else if(type == '01'){
            shLogin();
        }
    }
}

/**
 * 管理员账号登录
 */
function adminAcLogin() {
    var acAccount = $("#acAccount").val();
    var acPassword = $("#acPassword").val();
    var redirectUrl = $("#redirectUrl").val();
    //console.log(redirectUrl);
    if(acAccount == null || acAccount == '' ){
        showAdminErrorInfo("请输入账号");
        return;
    }
    if(acPassword == null || acPassword == '' ){
        showAdminErrorInfo("请输入密码");
        return;
    }
    if(acPassword.length < 6){
        showAdminErrorInfo("最少输入6位密码");
        return;
    }
    $("#adminAcLoginId").attr('disabled',true);
    // 账号密码提交
    $.ajax({
        type:"POST",
        url:"/check/admin/login",
        data:{
            account:acAccount,
            password: acPassword,
            redirect_url: redirectUrl
        },
        success:function(data) {
            //console.log(data);
            layer.closeAll();
            if (data.code=="100") {
                $("#acSubmit").attr('disabled',false);
                var url = data.data.callbackUrl;
                window.location.href = url.replace(/anchor/g,"#")
            } else {
                if(data.code == '9009'){
                    showAdminErrorInfo("账号不存在,请确认账号无误!");
                }else {
                    showAdminErrorInfo(data.mesg);
                }
                return;
            }
        },
        error:function() {
            showAdminErrorInfo("抱歉，登录出现异常，请稍后重试");
            $("#adminAcLoginId").attr('disabled',false);
            layer.closeAll();
        }
    });
}

/**
 * 账号登录
 */
function acLogin() {
    //console.log("登录验证");
    var acAccount = $("#acAccount").val();
    var acPassword = $("#acPassword").val();
    var acVcode = $("#acVcode").val();
    var redirectUrl = $("#redirectUrl").val();
    if(acAccount == null || acAccount == '' ){
        layer.msg("请输入账号");
        return;
    }
    if(acPassword == null || acPassword == '' ){
        layer.msg("请输入密码");
        return;
    }
    if(acPassword.length < 5){
        layer.msg("最少输入5位密码");
        return;
    }
    if(acVcode == null || acVcode == '' ){
        layer.msg("请输入图形验证码");
        return;
    }
    if (acVcode.length != 4) {
        layer.msg("请输入4位图形验证码");
        return;
    }
    $("#acSubmit").attr('disabled',true);
    // 账号密码提交
    $.ajax({
        type:"POST",
//        url:"/account/password/login",
        url:"/check/member/login/account",
        data:{
            account:acAccount,
            password: acPassword,
            vcode: acVcode,
            redirect_url: redirectUrl
        },
        success:function(data) {
            layer.closeAll();
            if (data.code=="100") {
                $("#acSubmit").attr('disabled',false);
            	var url = data.data.callbackUrl;
            	window.location.href = url;
//            	window.location.href = url.replace(/anchor/g,"#");
            } else {
                layer.msg(data.mesg);
                $("#acSubmit").attr('disabled',false);
                $("#acChangeImg").click();
            }
        },
        error:function() {
            layer.msg("抱歉，登录出现异常，请稍后重试");
            $("#acSubmit").attr('disabled',false);
            $("#acChangeImg").click();
            layer.closeAll();
        }
    });
}

/**
 * 税号登录
 */
function shLogin() {
    var shAccount = $("#shAccount").val();
    var shPassword = $("#shPassword").val();
    var shVcode = $("#shVcode").val();
    var redirectUrl = $("#redirectUrl").val();
    if(shAccount == null || shAccount == '' ){
        showErrorInfo('请输入纳税人识别号');
        return;
    }
    if(shPassword == null || shPassword == '' ){
        showErrorInfo('请输入密码');
        return;
    }
    if(shPassword.length < 5){
        showErrorInfo('最少输入5位密码');
        return;
    }
    if(shVcode == null || shVcode == '' ){
       showErrorInfo('请输入图形验证码');
       return;
    }
    if(shVcode.length != 4){
    	showErrorInfo('请输入4位图形验证码');
        return;
    }
    $("#shSubmit").attr('disabled',true);
    // 账号密码提交
    $.ajax({
        type:"POST",
        // url:"/nsrsbh/password/login",
        url:"/check/member/login/nsrsbh",
        data:{
            nsrsbh: shAccount,
            password: shPassword,
            vcode: shVcode,
            redirect_url: redirectUrl
        },
        dataType:"json",
        success:function(data){
            $("#shSubmit").attr('disabled',false);
            if (data.code=="100") {
            	// var obj = data.data;
            	// var url = obj.tokenUrl+"?sid="+obj.token+"&uid="+obj.userId+"&redirect_url="+obj.redirectUrl;
            	// window.location.href = url.replace(/anchor/g,"#");
                var url = data.data.callbackUrl;
                window.location.href = url.replace(/anchor/g,"#");
            } else {
                showErrorInfo(data.mesg);
                $("#shSubmit").attr('disabled',false);
                $("#shChangeImg").click();
            }
        },
        error:function(){
            showErrorInfo("抱歉，登录出现异常，请稍后重试");
            $("#shSubmit").attr('disabled',false);
            $("#shChangeImg").click();
        }
    });
}

/**
 * 获取短信验证码
 */
function getSMSMcode() {
    var mobile = $("#mobile").val();
    var password = $("#password").val();
    var vcode = $("#vcode").val();
    if(mobile == null || mobile == ''){
        layer.msg("请输入11位手机号");
        return;
    }
    if(!/^((13[0-9])|(14[5,6,7,8])|(15[^4,\\d])|(166)|(17[0-9])|(18[0-9])|(19[8,9]))\d{8}$/.test(mobile)){
        layer.msg("请输入正确的手机号");
        return;
    }
    if(password == null || password == '' ){
        layer.msg("请输入密码");
        return;
    }
    if(password.length < 6){
        layer.msg("最少输入6位密码");
        return;
    }
    if(vcode == null || vcode == '' ){
        layer.msg("请输入图形验证码");
        return;
    }
    if(vcode.length != 4){
        layer.msg("请输入4位图形验证码");
        return;
    }

    $("#smsBtn").attr('disabled',true);
    $.ajax({
        type:"post",
        url:"/register/sms/verification/code",
        data:{
            mobile: mobile,
            vcode: vcode
        },
        dataType:"json",
        success: function(data){
            if(data.code=="100"){
                layer.msg("短信发送成功", {icon: 1,time: 2000},function(){
                    getCode();
                });
            }else{
                layer.msg(data.mesg);
                $("#smsBtn").attr('disabled',false);
            }
        },
        error:	function(){
        	layer.msg("抱歉，发送短信出现异常，请稍后重试");
            $("#smsBtn").attr('disabled',false);
        }
    });
}

/**
 * 去注册新用户
 */
function doRegister() {
    var mobile = $("#mobile").val();
    var password = $("#password").val();
    var vcode = $("#vcode").val();
    var mcode = $("#mcode").val();
    if(mobile == null || mobile == '' ){
       layer.msg("请输入11位手机号");
       return;
    }
    if(!/^((13[0-9])|(14[5,6,7,8])|(15[^4,\\d])|(166)|(17[0-9])|(18[0-9])|(19[8,9]))\d{8}$/.test(mobile)){
        layer.msg("请输入正确的手机号");
        return;
    }
    if(password == null || password == '' ){
       layer.msg("请输入密码");
       return;
    }
    if(password.length < 6){
       layer.msg("最少输入6位密码");
       return;
    }
    if(vcode == null || vcode == '' ){
       layer.msg("请输入图形验证码");
       return;
    }
    if(vcode.length != 4){
        layer.msg("请输入4位图形验证码");
        return;
    }
    if(mcode == null || mcode == '' ){
       layer.msg("请输入短信验证码");
      return;
    }
    if(mcode.length != 6){
        layer.msg("请输入正确的短信验证码");
        return;
    }
    $("#regSubmit").attr('disabled',true);
    // 账号密码提交
    $.ajax({
        type:"POST",
        url:"/register/information/save",
        data:{
            mobile:mobile,
            password: password,
            vcode: vcode,
            mcode: mcode
        },
        success:function(data){
            layer.closeAll();
            $("#regSubmit").attr('disabled',false);
            if(data.code=="100"){
                layer.msg("注册成功", {icon: 1,time: 2500}, function() {
                    //parent.location.reload();
                    window.location.href="http://login.caishuib.com/member/login";
                });
            }else{
                layer.msg(data.mesg);
                $("#regSubmit").attr('disabled',false);
                $("#regChangeImg").click();
            }
        },
        error:function(){
            $("#regSubmit").attr('disabled',false);
            $("#regChangeImg").click();
            layer.msg("抱歉，发送短信出现异常，请稍后重试");
            layer.closeAll();
        }
    });

}

// 验证码倒计时函数
function getCode() {
    var countdown = 60;

    function codeTime() {
        if (countdown < 0) {
            $('.get-code').prop('disabled', false).text('重新发送');
            countdown = 60;
            clearInterval(time);
        } else {
            $('.get-code').prop('disabled', true).text('剩余' + countdown + ' s');
            countdown--;
        }
    }
    var time = setInterval(function () {
        codeTime()
    }, 1000);
}

/**
 * 获取找回密码短信验证码
 */
function getSMSfindPwdMcode() {
    var mobile = $("#mobile").val();
    var vcode = $("#vcode").val();
    if(mobile == null || mobile == ''){
        layer.msg("请输入11位手机号");
        return;
    }
    if(!/^((13[0-9])|(14[5,6,7,8])|(15[^4,\\d])|(166)|(17[0-9])|(18[0-9])|(19[8,9]))\d{8}$/.test(mobile)){
        layer.msg("请输入正确的手机号");
        return;
    }
    if(vcode == null || vcode == '' ){
        layer.msg("请输入图形验证码");
        return;
    }
    if(vcode.length != 4){
        layer.msg("请输入4位图形验证码");
        return;
    }

    $("#smsBtn").attr('disabled',true);
    $.ajax({
        type:"post",
        url:"/find/pwd/sms/verification/code",
        data:{
            mobile: mobile,
            vcode: vcode
        },
        dataType:"json",
        success: function(data){
            if(data.code=="100"){

                layer.msg("短信发送成功", {icon: 1,time: 2000},function(){
                    getCode();
                });
            }else{
                layer.msg(data.mesg);
                $("#smsBtn").attr('disabled',false);
            }
        },
        error:	function(){
            layer.msg("抱歉，发送短信出现异常，请稍后重试");
            $("#smsBtn").attr('disabled',false);
        }
    });
}

/**
 * 校验原手机
 */
function verificationMobile() {
    var uid = $("#uid").val();

    var mobile = $("#mobile").val();
    var vcode = $("#vcode").val();
    var mcode = $("#mcode").val();
    if(mobile == null || mobile == '' || mobile.length != 11){
        layer.msg("请输入11位手机号");
        return;
    }else if(!/^((13[0-9])|(14[5,6,7,8])|(15[^4,\\d])|(166)|(17[0-9])|(18[0-9])|(19[8,9]))\d{8}$/.test(mobile)){
        layer.msg("请输入正确的手机号！");
        return;
    }
    if(vcode == null || vcode == '' ){
        layer.msg("请输入图形验证码");
        return;
    }
    if(mcode == null || mcode == '' ){
        layer.msg("请输入短信验证码");
        return;
    }
    $("#findPwdBtn").attr('disabled',true);
    // 账号密码提交
    $.ajax({
        type:"POST",
        url:"/verify/original/phone",
        data:{
            mobile:mobile,
            vcode: vcode,
            mcode: mcode
        },
        success:function(data){
            layer.closeAll();
            $("#findPwdBtn").attr('disabled',true);
            if(data.code=="100"){
                var uid = data.object;
                layer.msg("手机号校验成功，跳转中", {icon: 1,time: 2500}, function() {
                    //parent.location.reload();
                    window.location.href="http://login.caishuib.com/reset/password?uid="+uid;
                });
            }else{
                layer.msg(data.mesg);
                $("#findPwdBtn").attr('disabled',false);
                $("#fpwdChangeImg").click();
            }
        },
        error:function(){
            $("#findPwdBtn").attr('disabled',false);
            $("#fpwdChangeImg").click();
            layer.msg("抱歉，校验原手机出现异常，请稍后重试");
            layer.closeAll();
        }
    });

}

/**
 * 重置账户密码
 */
function resetAccountPWD() {
    var newPassword = $("#newPassword").val();
    var verPassword = $("#verPassword").val();
    var uid = $("#uid").val();

    if(newPassword == null || newPassword == ''){
        layer.msg("请输入6-18位新密码，区分大小");
        return;
    }
    if(newPassword.length < 6 ){
    	layer.msg("最少输入6位密码");
        return;
    }
    if(verPassword == null || verPassword == '' ){
        layer.msg("请确认输入的新密码");
        return;
    }
    if(verPassword.length < 6){
        layer.msg("最少输入6位密码");
        return;
    }
    if(verPassword !== newPassword){
        layer.msg("两次输入的新密码不一致");
        return;
    }
    $("#restPwdBtn").attr('disabled',true);
    // 账号密码提交
    $.ajax({
        type:"POST",
        url:"/confirm/reset/password",
        data:{
            pwd:newPassword,
            uid:uid
        },
        success:function(data){
            console.log(data);
            layer.closeAll();
            $("#restPwdBtn").attr('disabled',false);
            if(data.code=="100"){
                layer.msg("重置密码成功", {icon: 1,time: 2500}, function() {
                    //parent.location.reload();
                    window.location.href="http://login.caishuib.com/member/login";
                });
            }else{
                layer.msg(data.mesg);
                $("#restPwdBtn").attr('disabled',false);
            }
        },
        error:function(){
            $("#restPwdBtn").attr('disabled',false);
            layer.msg("抱歉，重置密码出现异常，请稍后重试");
            layer.closeAll();
        }
    });
}